/**
 *	@author: Manimin Morga
 *	date created: January 5, 2006
 *	version: 1.09
 * modified: June 4, 2006
 * modification: getAbbrev : space before and after the word
 */

package text.ia;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import text.util.DBConnector;

public class TExtLexicon{

	Vector vResults;
	char cTarget;
	
	TExtMorphologyProcessor mp;
	
	public TExtLexicon(){
		vResults = new Vector();
		cTarget = 'e';
		
		mp = new TExtMorphologyProcessor(cTarget);	
	}
	
	public TExtLexicon(char cTarget){
		vResults = new Vector();

		this.cTarget = cTarget;
		mp = new TExtMorphologyProcessor(cTarget);	
	}
	
	/**	Constructs the query that will look for and return the translations of 
	 *		the given word (strWord).
	 * 
	 *	Returns the {@link Vector} of translations of strWord.
	 *
	 *	@param strWord a {@link String} whose translations are to be searched.
	 *	@return the {@link Vector} containing the translations of strWord
	 */
	public Vector lookupForAlignment(String strWord){
		String strQuery;
		String strTargetField;
		Vector vResult = null;
		Vector vCandidateMG;
		Vector vAll;
		Vector vMAResult;
		Enumeration eRootWords;
		boolean bRoot = true;
	
		if(cTarget == 'f'){
			strTargetField = "filipinoWord";
			strQuery = "Select " + strTargetField + " from Lexicon where englishWord = '" + strWord.replace("'", "''") + "'";
		}
		else{
			strTargetField = "englishWord";
			strQuery = "Select " + strTargetField + " from Lexicon where filipinoWord = '" + strWord.replace("'", "''") + "'";
		}	
		
		vResult = dbLookUp(strQuery, strTargetField);
		if(vResult == null){
			bRoot = false;
			vMAResult = mp.wordMA(strWord.replace("'", "''"), cTarget, false);
			if(vMAResult != null){
				strWord = (String)vMAResult.get(0);
				if(cTarget == 'f')
					strQuery = "Select " + strTargetField + " from Lexicon where englishWord = '" + strWord.replace("'", "''") + "'";
				else
					strQuery = "Select " + strTargetField + " from Lexicon where filipinoWord = '" + strWord.replace("'", "''") + "'";
				vResult = dbLookUp(strQuery, strTargetField);
			}
		}
		
		if(vResult == null)
			return null;
		else{
			vAll = (Vector)vResult.clone();
			eRootWords = ((Vector)vResult.clone()).elements();
			vResult = null;
			vResult = new Vector();
			while(eRootWords.hasMoreElements()){
				vCandidateMG = mp.wordMG(((String)eRootWords.nextElement()).replace("'", "''"), null);
				if(vCandidateMG != null)
					vResult.addAll(vCandidateMG);
			}
			if(!bRoot){
				vAll.addAll(0, vResult);
				vAll.add(new Integer(vResult.size()));
			}
			else{
				int nSize = vAll.size();
				vAll.addAll(vResult);
				vAll.add(new Integer(nSize));
			}
			if(vAll.size() > 0)
				return vAll;
		}
		return null;
	}
	
	/**	Constructs the query that will look for and return the translations of 
	 *		the given word (strWord).
	 *	Used in word-for-word translation, where the use of "forms" in MA and MG are
	 *		critical.
	 * 
	 *	Returns the {@link Vector} of translations of strWord.
	 *
	 *	@param strWord a {@link String} whose translations are to be searched.
	 *	@return the {@link Vector} containing the translations of strWord
	 */
	public Vector lookup(String strWord){
		String strQuery;
		String strTargetField;
		Vector vResult = null;
		Vector vCandidateMG;
		Vector vAll;
		Vector vMAResult;
		boolean bRoot = true;
		Vector vForms = null;
	
		if(cTarget == 'f'){
			strTargetField = "filipinoWord";
			strQuery = "Select " + strTargetField + " from Lexicon where englishWord = '" + strWord.replace("'", "''") + "'";
		}
		else{
			strTargetField = "englishWord";
			strQuery = "Select " + strTargetField + " from Lexicon where filipinoWord = '" + strWord.replace("'", "''") + "'";
		}	
		
		vResult = dbLookUp(strQuery, strTargetField);
		if(vResult == null){
			bRoot = false;
			vMAResult = mp.wordMA(strWord.replace("'", "''"), cTarget, true);
			if(vMAResult != null){
				vForms = (Vector)vMAResult.lastElement();
				vMAResult.remove(vMAResult.size()-1);
				
				vResult = new Vector();
				for(Iterator it = vMAResult.iterator(); it.hasNext(); ){
					strWord = (String)it.next();
					if(cTarget == 'f')
						strQuery = "Select " + strTargetField + " from Lexicon where englishWord = '" + strWord.replace("'", "''") + "'";
					else
						strQuery = "Select " + strTargetField + " from Lexicon where filipinoWord = '" + strWord.replace("'", "''") + "'";
					vResult.add(dbLookUp(strQuery, strTargetField));
				}
				
				if(vResult.size() == 0)	
					vResult = null;
			}
		}
		
		if(vResult == null)
			return null;
		if(bRoot)
			return vResult;
		else{
			Iterator itBlocks = ((Vector)vResult.clone()).iterator();
			Iterator itForms = vForms.iterator();
			
			vResult = null;
			vResult = new Vector();
			while(itBlocks.hasNext()){
				String strForm = (String)itForms.next();
				Vector vBlocks = (Vector)itBlocks.next();
				
				if(vBlocks == null)
					break;
					
				Iterator itRoot = vBlocks.iterator();
				
				while(itRoot.hasNext()){
					vCandidateMG = mp.wordMG(((String)itRoot.next()).replace("'", "''"), strForm);
					if(vCandidateMG != null)
						vResult.addAll(vCandidateMG);
				}
			}
			if(vResult.size() > 0)
				return vResult;
		}
		return null;
	}

	/*	Constructs the query that will check whether the given word (strWord) is a contraction
	 *		and returns its expanded form if it is so.
	 *	Only contractions in the same language as that of strWord will be considered.
	 * 
	 *	Returns the {@link Vector} of possible expanded forms of strWord
	 *
	 *	@param strWord a {@link String} whose contraction is to be searched for it is a contraction
	 *	@return the {@link Vector} containing the possible expanded forms
	 */
	public Vector lookupContraction(String strWord){
		String strLanguage = null;
		String strQuery = null;
		String strTargetField = null;
		Vector vResult = null;
		
		if(cTarget == 'e') {
			strLanguage = "Filipino";
		}else {
			strLanguage = "English";
		}
		
		strTargetField = "Separate";
		strQuery = "SELECT " + strTargetField + " FROM Contractions WHERE Contraction = '" + strWord + "' AND Language = '" + strLanguage + "'";
		
		vResult = dbLookUp(strQuery, strTargetField);

		return vResult;
	}

	/*	Checks if the given word (strWord) is an abbreviation.
	 *	Only abbreviations in the same language as that of strWord will be searched for.
	 * 
	 *	Returns a boolean value indicating whether strWord is an abbreviation (true) or not (false)
	 *
	 *	@param strWord a {@link String} that will searched for in the list of abbreviations to check if it is one
	 *	@return the boolean value indicating the classification of strWord
	 *		true - strWord is an abbreviation
	 *		false - strWord is not an abbreviation
	 */
	public boolean lookupAbbrev(String strWord){
		boolean isInDB = false;
		
		if(getAbbrev(strWord) != null) {
			isInDB = true;
		}
		
		return isInDB;
	}

	public Vector getAbbrev(String strWord){
		String strLanguage = null;
		String strQuery = null;
		String strTargetField = null;
		Vector vResult = null;
		
		if(cTarget == 'e') {
			strLanguage = "Filipino";
		}else {
			strLanguage = "English";
		}

		strTargetField = "Abbreviation";

		strQuery = "SELECT Abbreviation FROM AbbreviationsRE WHERE Language LIKE '%" + strLanguage + "%' AND ' " + strWord + " ' REGEXP Abbreviation";
		
		vResult = dbLookUp(strQuery, strTargetField);

		return vResult;
	}
	
	/**	Constructs the query that will look for and return the possible compound words
	 *		starting with the given word (strWord).
	 *	Only compound words in the same language as that of strWord will be searched for.
	 * 
	 *	Returns the {@link Vector} of possible compound words
	 *
	 *	@param strWord a {@link String} whose possible compound words are to be searched.
	 *	@return the {@link Vector} containing the possible compound words
	 */
	public Vector lookupCompound(String strWord){
		String strQuery;
		String strTargetField;
		Vector vResult = new Vector();
		Vector vTemp = null;
		Vector vInitial = null;
		
		if(cTarget == 'f'){
			strTargetField = "englishWord";
			strQuery = "Select englishWord from Lexicon where englishWord like '" + strWord.replace("'", "''") + " %'";
		}
		else{
			strTargetField = "filipinoWord";
			strQuery = "Select filipinoWord from Lexicon where filipinoWord like '" + strWord.replace("'", "''") + " %'";
		}
		
		vResult = dbLookUp(strQuery, strTargetField);
		
		if(vResult != null)
			vInitial = (Vector)vResult.clone();
		if(cTarget == 'f')
			vTemp = mp.wordMA(strWord.replace("'", "''") + " ", 'e', false);
		else
			vTemp = mp.wordMA(strWord.replace("'", "''") + " ", 'f', false);
		
		if(vTemp != null){
			if(vInitial == null)
				vInitial = new Vector();
			vInitial.addAll(vTemp);
		}
		return vInitial;
	}

	/**	Performs the actual DB lookup.
	 *	Returns the Vector of results.
	 */
	public Vector dbLookUp(String strQuery, String strTargetField){
		ResultSet rsEquivWords;
		
		rsEquivWords = DBConnector.select(strQuery);
		
		vResults.clear();
		
		try{
			while(rsEquivWords.next()){
				String strResult = rsEquivWords.getString(strTargetField);
				if(strResult != null)
					vResults.add(new String(strResult));
			}
		}	
		catch(SQLException e){}
		finally{
			try{
				rsEquivWords.close();
			}catch(Exception e){
				
			}
		}

		if(vResults.size() == 0)
			return null;
		return vResults;
	}
}